package com.sound.ampache;

/* Copyright (c) 2010 Brian Kirkman <kirkman.brian@gmail.com>
*
* +------------------------------------------------------------------------+
* | This program is free software; you can redistribute it and/or          |
* | modify it under the terms of the GNU General Public License            |
* | as published by the Free Software Foundation; either version 3         |
* | of the License, or (at your option) any later version.                 |
* |                                                                        |
* | This program is distributed in the hope that it will be useful,        |
* | but WITHOUT ANY WARRANTY; without even the implied warranty of         |
* | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          |
* | GNU General Public License for more details.                           |
* |                                                                        |
* | You should have received a copy of the GNU General Public License      |
* | along with this program; if not, write to the Free Software            |
* | Foundation, Inc., 59 Temple Place - Suite 330,                         |
* | Boston, MA  02111-1307, USA.                                           |
* +------------------------------------------------------------------------+
*
* Special thanks to the following: 
* Kevin Purdy, author of Amdroid, for much of the code used in this project
* 
* Jorge Ramon, author of MiamiCoder blog, for many great BlackBerry examples
* published on his blog.
* 
* 
*/

import net.rim.device.api.system.ApplicationDescriptor;
import net.rim.device.api.system.Characters;
import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.FieldChangeListener;
import net.rim.device.api.ui.Font;
import net.rim.device.api.ui.Manager;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.SeparatorField;
import net.rim.device.api.ui.component.TextField;
import net.rim.device.api.ui.container.PopupScreen;
import net.rim.device.api.ui.container.VerticalFieldManager;

public class HelpScreen extends PopupScreen{

    public HelpScreen() {
        super(new VerticalFieldManager(), Field.FOCUSABLE);
        this.setFont(Font.getDefault().derive(Font.PLAIN, 18));
        
        ApplicationDescriptor descriptor = ApplicationDescriptor.currentApplicationDescriptor();
        String appName = descriptor.getName();
        String appVersion = descriptor.getVersion();

        /* Construct the Close button */
        FieldChangeListener listener = new FieldChangeListener() {
         public void fieldChanged(Field field, int context) {
          ok();
         }
        };
        
        ButtonField b = new ButtonField("Close", Field.FIELD_HCENTER);
        b.setChangeListener(listener);

        /* Construct the text box containing the help */
        VerticalFieldManager vfm = new VerticalFieldManager(VERTICAL_SCROLL);
        vfm.add(createTextField(""));
        vfm.add(createTextField("Options:"));
        vfm.add(createTextField(""));
        vfm.add(createTextField("Enter the Ampache server address in the 'Server URL:' field.  " +
        		"This will typically be the URL used to connect to Ampache from outside the localhost " +
        		"or local network.  If the BlackBerry will be attached to the local network via a WiFi " +
        		"connection, a local network address may be used, though this will limit mobile flexibility."));
        vfm.add(createTextField(""));
        vfm.add(createTextField("Enter a valid Ampache username in the 'User:' field."));
        vfm.add(createTextField(""));
        vfm.add(createTextField("Enter the corresponding user password in the 'Password:' field."));
        vfm.add(createTextField(""));
        vfm.add(createTextField("The network connection settings can be adjusted from the " +
        		"'Connection:' dropdown list."));
        vfm.add(createTextField(""));
        vfm.add(createTextField("'Auto' will attempt to connect to the Ampache server by selecting " +
        		"the first available network connection method in the following order."));
        vfm.add(createTextField("   1. WiFi\n   2. Carrier\n   3. BES-MDS\n   4. Device"));
        vfm.add(createTextField("Additionally, the choices listed above can be selected manually."));
        vfm.add(createTextField(""));
        vfm.add(createTextField("'WiFi' will attempt to connect to the Ampache server using " +
        		"the device's WiFi connection.  If WiFi is selected, and an established " +
        		"connection does not exist or the device does not support WiFi, " +
        		"Ampacheberry will not connect to the Ampache server."));
        vfm.add(createTextField(""));
        vfm.add(createTextField("'Carrier' will attempt to connect to the Ampache server using the " +
        		"data provider's cellular network."));
        vfm.add(createTextField(""));
        vfm.add(createTextField("'BES-MDS' will attempt to connect to the Ampache server using a " +
        		"BlackBerry Enterprise Server (BES).  A BES is typically used to make data connections " +
        		"when a device has been issued as part of a corporate network.  If BES is selected, and " +
        		"the device is not set up to connect to a BES, Ampacheberry will not connect to the " +
        		"Ampache server."));
        vfm.add(createTextField(""));
        vfm.add(createTextField("'Device' will attempt to connect to the Ampache server in a direct " +
        		"connection method determined by the BlackBerry device.  Use this method if all other " +
        		"methods do not connect or are slow to connect."));
        vfm.add(createTextField(""));
        vfm.add(createTextField("'Font Size' will set the font size, and in turn the row height, " +
        		"for any screen displaying albums, artists, or songs.  This setting will not change the " +
        		"font size of the Home screen, the Tags screen, or the Playlists screen."));
        vfm.add(createTextField(""));
        vfm.add(createTextField("'Cache Albums and Artists', when checked, will cache the complete list " +
        		"of albums and artists when downloaded for the first time.  Every time the Artists or " +
        		"Albums screen is viewed thereafter, Ampacheberry will display the cached artists and albums. " +
        		"Any dynamic view of artists or albums, such as searching or viewing from tags, will " +
        		"not be queried from the cached items but instead queried from the Ampache server."));
        vfm.add(createTextField(""));
        vfm.add(createTextField("'Autoupdate Cache on Catalog Size Change', when checked, will update " +
        		"the cache if the number of albums and artists reported by Ampache differ from the number " +
        		"of artists and albums cached on the device.  This update will take place for cached " +
        		"albums when the Albums screen is viewed and for the cached artists when Artists screen " +
        		"is viewed.  When the albums or artists cache is autoupdated, the complete list will be " +
        		"downloaded."));
        vfm.add(createTextField(""));
        vfm.add(createTextField("'Clear Cache' button will clear the album and artist cache.  If 'Cache " +
        		"Albums and Artist' remains checked, the albums and artists will be cached next time " +
        		"they're downloaded."));
        vfm.add(createTextField(""));
        vfm.add(new SeparatorField());
        vfm.add(createTextField(""));
        vfm.add(createTextField("Notes:"));
        vfm.add(createTextField(""));
        vfm.add(createTextField(" - Ampache must be version 3.5 or higher."));
        vfm.add(createTextField(""));
        vfm.add(createTextField(" - Verify if the Ampache server requires a secure https connection.  The " +
        		"BlackBerry should be able to make a secure connection if necessary and will ask " +
        		"for connection confirmation and perhaps SSL certificate confirmation."));
        vfm.add(createTextField(""));
        vfm.add(createTextField(" - The Ampache API must be enabled for certain clients such as Ampacheberry " +
        		"to connect to Ampache.  Ampache versions prior to 3.5.4 must have an ACL entry of type " +
        		"API/RPC added.  See http://ampache.org/wiki/xmlapi for instructions to add the ACL entry.  " +
        		"Version 3.5.4 and higher should have this ACL entry included by default."));
        vfm.add(createTextField(""));
        vfm.add(createTextField(" - When 'Cache Albums and Artists' is selected, the cached items will " +
        		"be stored on the internal memory of the BlackBerry.  If the device memory is low, it " +
        		"might be beneficial to clear the cache and not cache the items."));
        vfm.add(createTextField(""));
        vfm.add(createTextField(" - Consult the Ampache forums and website " +
        		"http://ampache.org/wiki/config:transcode for information about " +
        		"transcoding. Consider setting up Ampache to downsample the music " +
        		"stream in order to save bandwidth."));
        vfm.add(createTextField(""));
        vfm.add(createTextField(" - Ampacheberry was originally developed as a playlist generator. " +
        		"Due to the fact that the built in BlackBerry Media Played seems to only play the " + 
        		"Ampache playlists on OS 5.0 devices, an integrated media player has now been added " +
        		"to Ampacheberry.  Since the integrated media player is newly developed, it still " +
        		"contains some known bugs. Please be patient and consult the Ampacheberry website for known bugs." +
        		"The option to save a playlist and play it from the built in BlackBerry Media Player still exists " +
        		"for those who find it useful.  If there is an issue regarding playlist playback, " + 
        		"the issue lies with the built in BlackBerry Media Player."));
        vfm.add(createTextField(""));
        vfm.add(new SeparatorField());
        vfm.add(createTextField(""));
        vfm.add(createTextField("Key Command Shortcuts:"));
        vfm.add(createTextField(""));
        vfm.add(createTextField("The following shortcuts exist on any screen displaying albums, artists, " +
        		"or songs."));
        vfm.add(createTextField("  T - Top of screen"));
        vfm.add(createTextField("  B - Bottom of screen"));
        vfm.add(createTextField("  Spacebar - Down one page"));
        vfm.add(createTextField("  N - Down five pages"));
        vfm.add(createTextField("  P - Up five Pages"));
        vfm.add(createTextField(""));
        vfm.add(new SeparatorField());
        vfm.add(createTextField(""));
        vfm.add(createTextField("About:"));
        vfm.add(createTextField(appName + " v" + appVersion));
        vfm.add(createTextField(""));
        vfm.add(b);

        /* Construct the screen */
        add(new LabelField("Ampacheberry Help", Field.FIELD_HCENTER));
        add(new SeparatorField());
        add(vfm);
       }

    
    private TextField createTextField(String content) {
    	TextField textField = new TextField(FIELD_LEFT | READONLY);
    	textField.setText(content);
    	return textField;
    }

    
    public void ok() {
    	UiApplication.getUiApplication().popScreen(this);
    }
    
    
    //Allows space bar to page down, T to top, B to bottom
    public boolean keyChar(char key, int status, int time) {
        switch (key) {
        case Characters.SPACE:
        	getScreen().scroll(Manager.DOWNWARD);
        	return true;
        case Characters.LATIN_SMALL_LETTER_T:
        	getScreen().scroll(Manager.TOPMOST);
        	return true;
        case Characters.LATIN_SMALL_LETTER_B:
        	getScreen().scroll(Manager.BOTTOMMOST);
        	return true;
        }
	        
        return super.keyChar(key, status, time);
    }

	
}
